package com.econ.powercloud.ems.service.dao.clickhouse;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.clickhouse.entity.EmsFaultData;
import com.econ.powercloud.clickhouse.mapper.EmsFaultDataMapper;
import com.econ.powercloud.ems.common.enums.AlarmOrFaultState;
import com.econ.powercloud.ems.common.enums.EventDealStatusType;
import com.econ.powercloud.ems.pojo.requestVO.EmsFaultRequestVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author jwc
 * @date 2022/10/31
 */
@Repository
@DS("clickhouse")
public class EmsFaultDataDAO extends ServiceImpl<EmsFaultDataMapper, EmsFaultData> {

    private final String LIMIT_ONE = "limit 1";

    public void insertBatch(List<EmsFaultData> list){
        this.getBaseMapper().insertBatch(list);
    }

    public Page<EmsFaultData> selectPageByCondition(EmsFaultRequestVO requestVO, List<String> energyStationIdList, int pageNo, int pageSize) {
        LambdaQueryWrapper<EmsFaultData> wrapper = Wrappers.<EmsFaultData>lambdaQuery()
                .le(EmsFaultData::getGmtCreate, requestVO.getEndTime()).ge(EmsFaultData::getGmtCreate, requestVO.getStartTime())
                .eq(StringUtils.isNotEmpty(requestVO.getId()), EmsFaultData::getId, requestVO.getId())
                .eq(StringUtils.isNotEmpty(requestVO.getFaultPid()), EmsFaultData::getFaultPid, requestVO.getFaultPid())
                .like(StringUtils.isNotEmpty(requestVO.getFaultName()), EmsFaultData::getFaultName, requestVO.getFaultName())
                .eq(requestVO.getFaultResource() != null, EmsFaultData::getFaultResource, requestVO.getFaultResource())
                .eq(StringUtils.isNotEmpty(requestVO.getEnergyEquipmentId()), EmsFaultData::getEnergyEquipmentId, requestVO.getEnergyEquipmentId())
                .eq(StringUtils.isNotEmpty(requestVO.getDeviceCode()), EmsFaultData::getDeviceCode, requestVO.getDeviceCode())
                .eq(requestVO.getDealState() != null, EmsFaultData::getDealState, requestVO.getDealState());
        if (requestVO.getFaultType() == null) {
            //正常状态过滤掉
            wrapper.ne(EmsFaultData::getFaultType, AlarmOrFaultState.NORMAL.getCode());
        } else {
            wrapper.eq(EmsFaultData::getFaultType, requestVO.getFaultType());
        }

        if (StringUtils.isNotEmpty(requestVO.getEnergyStationId())) {
            wrapper.eq(EmsFaultData::getEnergyStationId, requestVO.getEnergyStationId());
        } else if (CollectionUtils.isNotEmpty(energyStationIdList)) {
            wrapper.in(EmsFaultData::getEnergyStationId, energyStationIdList);
        }
        wrapper.orderByDesc(EmsFaultData::getGmtCreate);
        Page<EmsFaultData> page = new Page<>(pageNo, pageSize);
        return this.getBaseMapper().selectPage(page, wrapper);
    }

    public EmsFaultData selectOneByFaultPidAndFaultResource(String faultPid, Integer faultResource) {
        LambdaQueryWrapper<EmsFaultData> wrapper = Wrappers.<EmsFaultData>lambdaQuery()
                .eq(EmsFaultData::getFaultPid, faultPid)
                .eq(EmsFaultData::getFaultResource, faultResource)
                .orderByDesc(EmsFaultData::getGmtCreate)
                .last(LIMIT_ONE);
        return this.getBaseMapper().selectOne(wrapper);
    }

    public void insertOne(EmsFaultData obj) {
        this.getBaseMapper().insertOne(obj);
    }

    public void updateAllById(EmsFaultData obj) {
        this.getBaseMapper().updateAllById(obj);
    }

    public List<EmsFaultData> listByPid(List<String> faultPids,Integer faultResource) {
        LambdaQueryWrapper<EmsFaultData> wrapper = Wrappers.<EmsFaultData>lambdaQuery()
                .in(EmsFaultData::getFaultPid, faultPids)
                .eq(EmsFaultData::getFaultResource, faultResource)
                .ge(EmsFaultData::getGmtCreate,System.currentTimeMillis()- 30L *24*60*60*1000)
                .orderByDesc(EmsFaultData::getCreateTime);
        return this.getBaseMapper().selectList(wrapper);
    }

    public List<EmsFaultData> selectListByTime(EmsFaultRequestVO requestVO) {
        LambdaQueryWrapper<EmsFaultData> wrapper = Wrappers.<EmsFaultData>lambdaQuery()
                .le(EmsFaultData::getGmtCreate, requestVO.getEndTime())
                .ge(EmsFaultData::getGmtCreate, requestVO.getStartTime())
                .eq(EmsFaultData::getDealState, EventDealStatusType.UN_DEAL.getCode());
        return  this.getBaseMapper().selectList(wrapper);
    }

    public void updateDealStateAndGmtModifyOperatorId(Integer dealState, String operatorId, Long gmtModify, Long startTime, Long endTime) {
        this.getBaseMapper().updateDealStateAndGmtModifyOperatorId(dealState, operatorId, gmtModify, startTime, endTime);
    }
}
